import os
import math
import pylab as plt
from params import *


x,y,c = [],[],[]
#file = os.path.join(WORKDIR, 'gowalla', 'temporal', 'data','gowalla_degree_distribution.txt')
#for line in open(file):
#    deg,freq,acc = map(float,line.strip().split())
#    x.append(deg)
#    y.append(freq)
#    c.append(acc)

file = os.path.join(WORKDIR, 'gsn','results', 'gowalla' ,'gowalla_degree_distribution.txt')
acc = 0
for line in open(file):
    a,b = map(int,line.strip().split(';'))
    a = float(a)
    x.append(a)
    y.append(b)
    acc += b
    c.append(acc)

tot = c[-1]
c = [1-1.0*i/tot for i in c]

plt.figure()
plt.clf()
ax = plt.axes(FIG_AXES2)
plt.loglog(x,c,'ko',mfc="None")
plt.grid(True)
plt.xlabel("Node degree")
plt.ylabel('CCDF')
plt.xlim(xmin=1,xmax=1e4)
plt.ylim(ymin=1e-5,ymax=1)
plt.savefig('temporal_gowalla_degree_dist.pdf')
plt.close()

file = os.path.join(WORKDIR, 'gowalla', 'temporal', 'data','gowalla_user_span.txt')

spans = {}
MAX_AGE=500
avg = []
for line in open(file):
    n,age = map(int,line.strip().split())
    if age <= MAX_AGE:
        spans.setdefault(age,0)
        spans[age] += 1
        avg.append(age)


avg = float(sum(avg))/len(avg)
print "average lifespan ", avg

x = sorted(spans)
y = [spans[k] for k in x]

c = [0.0]
for i in y:
    c.append(c[-1]+i)

tot = c[-1]
c = [1.0-i/tot for i in c[:-1]]
#plt.figure()
#plt.clf()
#ax = plt.axes(FIG_AXES2)
#plt.semilogy(x,y,'ko',mfc="None")
#plt.grid(True)
#plt.xlabel("Node lifespan (days)")
#plt.ylabel('PDF')
#plt.savefig('temporal_gowalla_span_freq.pdf')
#plt.close()

plt.figure()
plt.clf()
ax = plt.axes(FIG_AXES2)
plt.semilogy(x,c,'ko',mfc="None")
xfit = x
yfit = [math.log(i) for i in c]
a,b = plt.polyfit(xfit,yfit,1)
print "Theoretical %f, empirical %f"%(-1.0/a, avg)
fit = [math.exp(i) for i in plt.polyval([a,b],xfit)]
plt.semilogy(x,fit,'k--',linewidth=2)
plt.grid(True)
plt.ylim(ymin=1e-4,ymax=1)
plt.xlim(xmax=500)
plt.xlabel("Node lifespan (days)")
plt.ylabel('CCDF')
plt.savefig('temporal_gowalla_span_dist.pdf')
plt.close()

file = os.path.join(WORKDIR, 'gowalla', 'temporal', 'data','gowalla_user_age.txt')

ages = {}
MAX_AGE=1350
for line in open(file):
    n,age = map(int,line.strip().split())
    if age <= MAX_AGE:
        ages.setdefault(age,0)
        ages[age] += 1

x = sorted(ages)
y = [ages[k] for k in x]

c = [0.0]
for i in y:
    c.append(c[-1]+i)

tot = c[-1]
c = [1.0-i/tot for i in c[:-1]]

#plt.figure()
#plt.clf()
#ax = plt.axes(FIG_AXES2)
#plt.semilogy(x,y,'ko',mfc="None")
#plt.grid(True)
#plt.xlabel("Node age (days)")
#plt.ylabel('PDF')
#plt.savefig('temporal_gowalla_age_freq.pdf')
#plt.close()

plt.figure()
plt.clf()
ax = plt.axes(FIG_AXES2)
plt.semilogy(x,c,'ko',mfc="None")
#plt.plot(x,c,'ko')
plt.grid(True)
#plt.xlim(xmin=1,xmax=350)
plt.ylim(ymin=1e-5,ymax=1)
plt.xlabel("Node age (days)")
plt.ylabel('CCDF')
plt.savefig('temporal_gowalla_age_dist.pdf')
plt.close()

file = os.path.join(WORKDIR, 'gowalla', 'temporal', 'data',
    'gowalla_link_distance_distribution_linear.txt')

ages = {}
x,y,c = [],[],[]
for line in open(file):
    dist,freq,ccdf = map(float,line.strip().split())
    x.append(dist)
    y.append(freq)
    c.append(ccdf)

plt.figure()
plt.clf()
ax = plt.axes(FIG_AXES2)
plt.semilogy(x,c,'ko',mfc="None")
plt.grid(True)
plt.xlim(xmin=1,xmax=20000)
#plt.xlim(yxmin=1-5,ymax=1)
plt.xlabel("Link length (km)")
plt.ylabel('CCDF')
plt.savefig('temporal_gowalla_link_dist.pdf')
plt.close()
